package routers

import (
	"beego/controllers/admin"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func init() {
	ns :=
		beego.NewNamespace("/"+beego.AppConfig.String("adminPath"),
			//中间件:匹配路由前会执,可以用于权限验证
			//注意引入的包： github.com/astaxie/beego/context

			beego.NSBefore(func(ctx *context.Context) {
				pathname := ctx.Request.URL.String()
				adminPath := beego.AppConfig.String("adminPath")
				adminId := ctx.Input.Session("admin_id")
				fmt.Println("-------------------")
				fmt.Println(adminId)
				fmt.Println("-------------------")
				if adminId == nil {
					if pathname != "/"+adminPath+"/login" && pathname != "/"+adminPath+"/DoLogin" {
						ctx.Redirect(302, "/"+adminPath+"/login")
					}
				}
			}),

			//登录
			beego.NSRouter("/login", &admin.LoginController{}),
			beego.NSRouter("/DoLogin", &admin.LoginController{}, "post:DoLogin"),
			beego.NSRouter("/loginOut", &admin.LoginController{}, "get:LoginOut"),

			//首页
			beego.NSRouter("/index/index", &admin.IndexController{}),
			beego.NSRouter("/index/welcome", &admin.IndexController{}, "get:Welcome"),
			beego.NSRouter("/index/welcomes", &admin.IndexController{}, "get:Welcomes"),

			//角色
			beego.NSRouter("/role/index", &admin.RoleController{}),
			beego.NSRouter("/role/add", &admin.RoleController{}, "get:Add"),
			beego.NSRouter("/role/doadd", &admin.RoleController{}, "post:DoAdd"),
			beego.NSRouter("/role/edit", &admin.RoleController{}, "get:Edit"),
			beego.NSRouter("/role/doedit", &admin.RoleController{}, "post:DoEdit"),
			beego.NSRouter("/role/delete", &admin.RoleController{}, "get:Delete"),
			beego.NSRouter("/role/role", &admin.RoleController{}, "get:Role"),
			beego.NSRouter("/role/dorole", &admin.RoleController{}, "post:DoRole"),

			//管理员
			beego.NSRouter("/admin/index", &admin.AdminController{}),
			beego.NSRouter("/admin/add", &admin.AdminController{}, "get:Add"),
			beego.NSRouter("/admin/doadd", &admin.AdminController{}, "post:DoAdd"),
			beego.NSRouter("/admin/edit", &admin.AdminController{}, "get:Edit"),
			beego.NSRouter("/admin/doedit", &admin.AdminController{}, "post:DoEdit"),
			beego.NSRouter("/admin/delete", &admin.AdminController{}, "get:Delete"),

			//商品
			beego.NSRouter("/goods/index", &admin.GoodsController{}),
			beego.NSRouter("/goods/add", &admin.GoodsController{}, "get:Add"),
			beego.NSRouter("/goods/doadd", &admin.GoodsController{}, "post:DoAdd"),
			beego.NSRouter("/goods/doupload", &admin.GoodsController{}, "post:DoUpload"),
			beego.NSRouter("/goods/delete", &admin.GoodsController{}, "get:Delete"),
			beego.NSRouter("/goods/edit", &admin.GoodsController{}, "get:Edit"),
			beego.NSRouter("/goods/doedit", &admin.GoodsController{}, "post:DoEdit"),

			//网络通信
			beego.NSRouter("/network/index", &admin.NetworkController{}),
			beego.NSRouter("/network/join", &admin.NetworkController{}, "get:Join"),
		)
	//注册 namespace
	beego.AddNamespace(ns)
}
